#ifndef lint
static const char yysccsid[] = "@(#)yaccpar	1.9 (Berkeley) 02/21/93";
#endif

#include <stdlib.h>
#include <string.h>

#define YYBYACC 1
#define YYMAJOR 1
#define YYMINOR 9
#define YYPATCH 20070509

#define YYEMPTY (-1)
#define yyclearin    (yychar = YYEMPTY)
#define yyerrok      (yyerrflag = 0)
#define YYRECOVERING (yyerrflag != 0)

extern int yyparse(void);

static int yygrowstack(void);
#define yyparse ucesparse
#define yylex uceslex
#define yyerror uceserror
#define yychar uceschar
#define yyval ucesval
#define yylval uceslval
#define yydebug ucesdebug
#define yynerrs ucesnerrs
#define yyerrflag uceserrflag
#define yyss ucesss
#define yyssp ucesssp
#define yyvs ucesvs
#define yyvsp ucesvsp
#define yylhs uceslhs
#define yylen uceslen
#define yydefred ucesdefred
#define yydgoto ucesdgoto
#define yysindex ucessindex
#define yyrindex ucesrindex
#define yygindex ucesgindex
#define yytable ucestable
#define yycheck ucescheck
#define yyname ucesname
#define yyrule ucesrule
#define YYPREFIX "uces"
#line 2 "parser.y"
# include "parser.h"
static ChainingHashMap *result;
extern FILE *ucesin;
#line 7 "parser.y"
typedef union {
	char *string;
	float virgula;
	int digits;
	char *number;
	ChainingHashMap *map;
	UCE *uce;
} YYSTYPE;
#line 60 "uces.tab.c"
#define STRING 257
#define DIGITS 258
#define FLOAT 259
#define NUMBER 260
#define LABEL_UCE 261
#define LABEL_NAME 262
#define LABEL_MAXSTUDENTS 263
#define OPEN 264
#define CLOSE 265
#define EQUAL 266
#define STOP 267
#define YYERRCODE 256
short uceslhs[] = {                                        -1,
    0,    0,    1,    1,    2,    3,    3,    4,
};
short uceslen[] = {                                         2,
    1,    0,    2,    1,    4,    2,    0,    4,
};
short ucesdefred[] = {                                      0,
    0,    0,    0,    4,    7,    3,    0,    0,    5,    6,
    0,    0,    8,
};
short ucesdgoto[] = {                                       2,
    3,    4,    7,   10,
};
short ucessindex[] = {                                   -256,
 -261,    0, -256,    0,    0,    0, -263, -262,    0,    0,
 -253, -260,    0,
};
short ucesrindex[] = {                                      6,
    0,    0,    8,    0,    0,    0,    0,    0,    0,    0,
    0,    0,    0,
};
short ucesgindex[] = {                                      0,
    0,    7,    0,    0,
};
#define YYTABLESIZE 10
short ucestable[] = {                                       8,
    1,    9,    5,   11,   12,    2,   13,    1,    0,    6,
};
short ucescheck[] = {                                     263,
  257,  265,  264,  266,  258,    0,  267,    0,   -1,    3,
};
#define YYFINAL 2
#ifndef YYDEBUG
#define YYDEBUG 0
#endif
#define YYMAXTOKEN 267
#if YYDEBUG
char *ucesname[] = {
"end-of-file",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"STRING","DIGITS","FLOAT",
"NUMBER","LABEL_UCE","LABEL_NAME","LABEL_MAXSTUDENTS","OPEN","CLOSE","EQUAL",
"STOP",
};
char *ucesrule[] = {
"$accept : Document",
"Document : Uces",
"Document :",
"Uces : Uces Uce",
"Uces : Uce",
"Uce : STRING OPEN UceDataList CLOSE",
"UceDataList : UceDataList UceMaxStudents",
"UceDataList :",
"UceMaxStudents : LABEL_MAXSTUDENTS EQUAL DIGITS STOP",
};
#endif
#if YYDEBUG
#include <stdio.h>
#endif

/* define the initial stack-sizes */
#ifdef YYSTACKSIZE
#undef YYMAXDEPTH
#define YYMAXDEPTH  YYSTACKSIZE
#else
#ifdef YYMAXDEPTH
#define YYSTACKSIZE YYMAXDEPTH
#else
#define YYSTACKSIZE 500
#define YYMAXDEPTH  500
#endif
#endif

#define YYINITSTACKSIZE 500

int      yydebug;
int      yynerrs;
int      yyerrflag;
int      yychar;
short   *yyssp;
YYSTYPE *yyvsp;
YYSTYPE  yyval;
YYSTYPE  yylval;

/* variables for the parser stack */
static short   *yyss;
static short   *yysslim;
static YYSTYPE *yyvs;
static int      yystacksize;
#line 50 "parser.y"

static int uceEqual (void *a, void *b) {
	UCE *x = (UCE *) a;
	UCE *y = (UCE *) b;
	if (!strcmp(x->name,y->name)) return 1;
	else return 0;
}

static void insertUce (UCE *uce) {
	if (!putChaining(result,uce->name,uce)) {
		logMessage("io.files.uces","parser.y","insertUce","Elemento não foi inserido na hash",ERROR);
	}
}
static UCE * insertName(char *name, UCE *uce) {
	if (!uce) uce = newUCE();
	uce->name = name;
	return uce;
}
static UCE * insertMax(UCE *uce, int max) {
	if (!uce) uce = newUCE();
	uce->maxStudents = max;
	return uce;
}

/*int yywrap () {
	return 1;
}*/

int yyerror (char *err) {
	printf("%s\n",err);
	return 1;
}
int equalityString (void *a, void *b) {
	return !strcmp(a,b);
}
ChainingHashMap * readUCEs (FILE *fp) {
	ucesin = fp;
	result = newChainingHashMap(UCE_HASHMAP_SIZE);
	result->hash = portugueseHash;
	result->eq = equalityString;
	if (yyparse()) {
		printf("ERRO\n");
		return NULL;
	}
	return result; 
}

void writeUCEs (ChainingHashMap *map, FILE *fp) {
	LinkedList *keyList = keySetChaining(map);
	UCE *tmp;
	for (;keyList;keyList=keyList->next) {
		tmp = getChaining(map,getData(keyList));
		fprintf(fp,"'%s' {\n",tmp->name);
		if (tmp->maxStudents != 0 )
			fprintf(fp,"\tmaxAlunos=%d;\n",tmp->maxStudents);
		fprintf(fp,"}\n");
	}
}
#line 225 "uces.tab.c"
/* allocate initial stack or double stack size, up to YYMAXDEPTH */
static int yygrowstack(void)
{
    int newsize, i;
    short *newss;
    YYSTYPE *newvs;

    if ((newsize = yystacksize) == 0)
        newsize = YYINITSTACKSIZE;
    else if (newsize >= YYMAXDEPTH)
        return -1;
    else if ((newsize *= 2) > YYMAXDEPTH)
        newsize = YYMAXDEPTH;

    i = yyssp - yyss;
    newss = (yyss != 0)
          ? (short *)realloc(yyss, newsize * sizeof(*newss))
          : (short *)malloc(newsize * sizeof(*newss));
    if (newss == 0)
        return -1;

    yyss  = newss;
    yyssp = newss + i;
    newvs = (yyvs != 0)
          ? (YYSTYPE *)realloc(yyvs, newsize * sizeof(*newvs))
          : (YYSTYPE *)malloc(newsize * sizeof(*newvs));
    if (newvs == 0)
        return -1;

    yyvs = newvs;
    yyvsp = newvs + i;
    yystacksize = newsize;
    yysslim = yyss + newsize - 1;
    return 0;
}

#define YYABORT goto yyabort
#define YYREJECT goto yyabort
#define YYACCEPT goto yyaccept
#define YYERROR goto yyerrlab
int
yyparse(void)
{
    register int yym, yyn, yystate;
#if YYDEBUG
    register const char *yys;

    if ((yys = getenv("YYDEBUG")) != 0)
    {
        yyn = *yys;
        if (yyn >= '0' && yyn <= '9')
            yydebug = yyn - '0';
    }
#endif

    yynerrs = 0;
    yyerrflag = 0;
    yychar = YYEMPTY;

    if (yyss == NULL && yygrowstack()) goto yyoverflow;
    yyssp = yyss;
    yyvsp = yyvs;
    *yyssp = yystate = 0;

yyloop:
    if ((yyn = yydefred[yystate]) != 0) goto yyreduce;
    if (yychar < 0)
    {
        if ((yychar = yylex()) < 0) yychar = 0;
#if YYDEBUG
        if (yydebug)
        {
            yys = 0;
            if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
            if (!yys) yys = "illegal-symbol";
            printf("%sdebug: state %d, reading %d (%s)\n",
                    YYPREFIX, yystate, yychar, yys);
        }
#endif
    }
    if ((yyn = yysindex[yystate]) && (yyn += yychar) >= 0 &&
            yyn <= YYTABLESIZE && yycheck[yyn] == yychar)
    {
#if YYDEBUG
        if (yydebug)
            printf("%sdebug: state %d, shifting to state %d\n",
                    YYPREFIX, yystate, yytable[yyn]);
#endif
        if (yyssp >= yysslim && yygrowstack())
        {
            goto yyoverflow;
        }
        *++yyssp = yystate = yytable[yyn];
        *++yyvsp = yylval;
        yychar = YYEMPTY;
        if (yyerrflag > 0)  --yyerrflag;
        goto yyloop;
    }
    if ((yyn = yyrindex[yystate]) && (yyn += yychar) >= 0 &&
            yyn <= YYTABLESIZE && yycheck[yyn] == yychar)
    {
        yyn = yytable[yyn];
        goto yyreduce;
    }
    if (yyerrflag) goto yyinrecovery;

    yyerror("syntax error");

#ifdef lint
    goto yyerrlab;
#endif

yyerrlab:
    ++yynerrs;

yyinrecovery:
    if (yyerrflag < 3)
    {
        yyerrflag = 3;
        for (;;)
        {
            if ((yyn = yysindex[*yyssp]) && (yyn += YYERRCODE) >= 0 &&
                    yyn <= YYTABLESIZE && yycheck[yyn] == YYERRCODE)
            {
#if YYDEBUG
                if (yydebug)
                    printf("%sdebug: state %d, error recovery shifting\
 to state %d\n", YYPREFIX, *yyssp, yytable[yyn]);
#endif
                if (yyssp >= yysslim && yygrowstack())
                {
                    goto yyoverflow;
                }
                *++yyssp = yystate = yytable[yyn];
                *++yyvsp = yylval;
                goto yyloop;
            }
            else
            {
#if YYDEBUG
                if (yydebug)
                    printf("%sdebug: error recovery discarding state %d\n",
                            YYPREFIX, *yyssp);
#endif
                if (yyssp <= yyss) goto yyabort;
                --yyssp;
                --yyvsp;
            }
        }
    }
    else
    {
        if (yychar == 0) goto yyabort;
#if YYDEBUG
        if (yydebug)
        {
            yys = 0;
            if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
            if (!yys) yys = "illegal-symbol";
            printf("%sdebug: state %d, error recovery discards token %d (%s)\n",
                    YYPREFIX, yystate, yychar, yys);
        }
#endif
        yychar = YYEMPTY;
        goto yyloop;
    }

yyreduce:
#if YYDEBUG
    if (yydebug)
        printf("%sdebug: state %d, reducing by rule %d (%s)\n",
                YYPREFIX, yystate, yyn, yyrule[yyn]);
#endif
    yym = yylen[yyn];
    if (yym)
        yyval = yyvsp[1-yym];
    else
        memset(&yyval, 0, sizeof yyval);
    switch (yyn)
    {
case 1:
#line 31 "parser.y"
{ ; }
break;
case 2:
#line 32 "parser.y"
{ ; }
break;
case 3:
#line 35 "parser.y"
{ insertUce(yyvsp[0].uce); }
break;
case 4:
#line 36 "parser.y"
{ insertUce(yyvsp[0].uce); }
break;
case 5:
#line 39 "parser.y"
{ yyval.uce = insertName(yyvsp[-3].string,yyvsp[-1].uce); }
break;
case 6:
#line 42 "parser.y"
{ yyval.uce = insertMax(yyvsp[-1].uce,yyvsp[0].digits); }
break;
case 7:
#line 43 "parser.y"
{ yyval.uce = NULL; }
break;
case 8:
#line 46 "parser.y"
{ yyval.digits = yyvsp[-1].digits; }
break;
#line 438 "uces.tab.c"
    }
    yyssp -= yym;
    yystate = *yyssp;
    yyvsp -= yym;
    yym = yylhs[yyn];
    if (yystate == 0 && yym == 0)
    {
#if YYDEBUG
        if (yydebug)
            printf("%sdebug: after reduction, shifting from state 0 to\
 state %d\n", YYPREFIX, YYFINAL);
#endif
        yystate = YYFINAL;
        *++yyssp = YYFINAL;
        *++yyvsp = yyval;
        if (yychar < 0)
        {
            if ((yychar = yylex()) < 0) yychar = 0;
#if YYDEBUG
            if (yydebug)
            {
                yys = 0;
                if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
                if (!yys) yys = "illegal-symbol";
                printf("%sdebug: state %d, reading %d (%s)\n",
                        YYPREFIX, YYFINAL, yychar, yys);
            }
#endif
        }
        if (yychar == 0) goto yyaccept;
        goto yyloop;
    }
    if ((yyn = yygindex[yym]) && (yyn += yystate) >= 0 &&
            yyn <= YYTABLESIZE && yycheck[yyn] == yystate)
        yystate = yytable[yyn];
    else
        yystate = yydgoto[yym];
#if YYDEBUG
    if (yydebug)
        printf("%sdebug: after reduction, shifting from state %d \
to state %d\n", YYPREFIX, *yyssp, yystate);
#endif
    if (yyssp >= yysslim && yygrowstack())
    {
        goto yyoverflow;
    }
    *++yyssp = yystate;
    *++yyvsp = yyval;
    goto yyloop;

yyoverflow:
    yyerror("yacc stack overflow");

yyabort:
    return (1);

yyaccept:
    return (0);
}
